﻿define(function (require) {

    var DateView = require('view/DateView');
    var UpdateView = require('view/UpdateView');
    var NotificationView = require('view/NotificationView');
    var utility = require('ezdesk/utility/utility.js');

    return Backbone.View.extend({
        el: $(".ui-listview"),
        events: {
            //            "click .feed-close": "clear"
        },

        initialize: function () {
            _.bindAll(this, 'addOne', 'addAll', 'render', 'refresh');
            this.model.bind('add', this.render);
            this.model.bind('remove', this.render);
            this.model.bind('all', this.refresh);
        },
        date: null,
        refresh: function () {

        },
        render: function () {
            $(".ui-listview").empty();
            this.date = null;
            this.addAll();
            if (this.model.models.length == 0) this.trigger("empty");
            var unreadFeedCount = this.model.unread().length;
            $.postMessage('unreadFeeds:' + unreadFeedCount, '*', parent);
        },
        addEl: function (view) {
            var element = view.render().el;
            this.el.append(element);
        },
        addOne: function (e) {
            var _feedListView = this;
            var currentDay = utility.GetCurrentDay(e.get("UpdateOn"));

            if (e.get('Type') == "notification") {
                var view = new NotificationView({ model: e });
                this.addEl(view);
            } else if (e.get('Type') == "update") {

                if (this.date != currentDay) {
                    var view = new DateView({ model: e });
                    this.addEl(view);
                    this.date = currentDay;
                }

                var view = new UpdateView({ model: e });
                this.addEl(view);
            }
        },

        addAll: function () {
            this.model.each(this.addOne);
        },
        noFeedCallback: function () {

        }

    });

});